package com.yang.jxj.service.impl;

import ch.qos.logback.core.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.jxj.entity.Students;
import com.yang.jxj.entity.User;
import com.yang.jxj.mapper.UserMapper;
import com.yang.jxj.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;

import static com.yang.jxj.Utils.AesUtil.encrypt;
import static com.yang.jxj.Utils.JwtUtil.createToken;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yang
 * @since 2024-06-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    public HashMap<String, Object> Alogin(User user) {
        HashMap<String, Object> hash = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        String userName = user.getUserName();
        String passwd = user.getPasswd();
        System.out.println((userName + passwd));
        if(StringUtil.isNullOrEmpty(userName) || StringUtil.isNullOrEmpty(passwd)) {
            hash.put("code","888");
            hash.put("msg","用户名或密码不正确");
            return hash;
        }else {
            queryWrapper.eq("user_name", userName);
            queryWrapper.eq("passwd", encrypt(passwd));
            User one = null;
            try {
                one = getOne(queryWrapper);

            } catch (Exception e) {
                hash.put("code", "999");
                hash.put("msg", "登陆失败");
            }
            if (one != null) {
                String token = createToken(one);
                hash.put("code", "000");
                hash.put("msg", "登陆成功");
                hash.put("token", token);
                return hash;
            } else {
                hash.put("code", "999");
                hash.put("msg", "用户名或密码不正确");
                return hash;
            }
        }
    }

    public HashMap<String, Object> loginById(User user) {
        HashMap<String, Object> hash = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        Integer userId = user.getUserId();
        String passwd = user.getPasswd();
        Integer role =  user.getUserRole();
        if(StringUtil.isNullOrEmpty(String.valueOf(userId) )  || StringUtil.isNullOrEmpty(passwd)) {
            hash.put("code","888");
            hash.put("msg","用户名或密码不正确");
            return hash;
        }else {
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("passwd", encrypt(passwd));
            queryWrapper.eq("user_role", role);
            User one = null;
            try {
                one = getOne(queryWrapper);
                System.out.println((one));
            } catch (Exception e) {
                hash.put("code", "999");
                hash.put("msg", "登陆失败");
            }
            if (one != null) {
                String token = createToken(one);
                hash.put("code", "000");
                hash.put("msg", "登陆成功");
                hash.put("token", token);
                return hash;
            } else {
                hash.put("code", "999");
                hash.put("msg", "用户名或密码不正确");
                return hash;
            }
        }
    }


}
